/*
 Xern Entity Relocation Network
 Copyright (C) 2011 Ronie Salgado <roniesalg@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

#ifndef XERN_CLIENT_SESSION_HPP
#define XERN_CLIENT_SESSION_HPP

#include <deque>
#include <boost/thread.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include "Xern/DispatchTable.hpp"
#include "Xern/IMaterial.hpp"
#include "Session.hpp"
#include "MetaverseNode.hpp"
#include "RemoteObject.hpp"
#include "RemoteRegion.hpp"
#include "RemotePlayer.hpp"
#include "TerrainCodec.hpp"
#include "TransferManager.hpp"

namespace XernMetaverse
{

	class ClientSession: public boost::enable_shared_from_this<ClientSession>
	{
	public:
		static const int TerrainSize = 256;
		static const int TerrainHeightMapSize = TerrainSize*TerrainSize*sizeof(float);
		static const int TerrainPatches = 16*16;
		static const posix_time::time_duration TerrainFinishTime;

		ClientSession(boost::shared_ptr<Session> session, boost::shared_ptr<MetaverseNode> node);
		~ClientSession();

		void Connected(boost::shared_ptr<Message> regionHandshake);
		void Disconnected();

		void Frame(float delta);
		void ReceiveMessage(boost::shared_ptr<Message> message);

		boost::shared_ptr<Entity> CreatePlayerController(boost::shared_ptr<Region> region);
		boost::shared_ptr<Message> CreateMessage(const std::string &name);

		void Send(boost::shared_ptr<Message> message);
		const uuid &GetId() const;
		const uuid &GetAgentId() const;

	private:
		void InitializeRegion(boost::shared_ptr<Message> handshake);
		void CalculateTerrainNormals();

		// Early messages.
		void OnLayerData(boost::shared_ptr<Message> message);

		// Late messages.
		void OnAgentMovementComplete(boost::shared_ptr<Message> message);
		void OnAvatarAnimation(boost::shared_ptr<Message> message);
		void OnAvatarAppearance(boost::shared_ptr<Message> message);
		void OnObjectUpdate(boost::shared_ptr<Message> message);
		void OnCoarseLocationUpdate(boost::shared_ptr<Message> message);
		void OnImprovedTerseObjectUpdate(boost::shared_ptr<Message> message);
		void OnKillObject(boost::shared_ptr<Message> message);
		void OnParcelOverlay(boost::shared_ptr<Message> message);
		void OnSimulatorViewerTimeMessage(boost::shared_ptr<Message> message);
		void OnSimStats(boost::shared_ptr<Message> message);
		void OnViewerEffect(boost::shared_ptr<Message> message);

		// Helper functions.
		void UpdatePrimitive(boost::shared_ptr<Entity> parent, boost::shared_ptr<Message> message, int subblock);
		void ReadTexture(boost::shared_ptr<Message> message, int subblock, boost::shared_ptr<IMaterial> material);

		boost::shared_ptr<Session> session;
		boost::shared_ptr<MetaverseNode> node;

		boost::weak_ptr<RemoteRegion> regionEntity;

		// Message queue
		typedef std::deque<boost::shared_ptr<Message> > MessageQueue;
		MessageQueue messageQueue;

		// Local object map.
		typedef std::map<int, boost::weak_ptr<RemoteObject> > LocalObjectMap;
		LocalObjectMap localObjectMap;

		// Dispatch tables.
		DispatchTable<ClientSession, void (boost::shared_ptr<Message>)> earlyMessageTable;
		DispatchTable<ClientSession, void (boost::shared_ptr<Message>)> lateMessageTable;

		// Sun.
		boost::shared_ptr<ILight> sun;

		// Terrain data.
		boost::shared_ptr<IGeometry> terrain;
		boost::shared_ptr<IMaterial> terrainMaterial;
		boost::array<float, TerrainHeightMapSize> terrainHeightMap;
		bool dirtyTerrain;
		bool dirtyTerrainNormals;
		TerrainCodec terrainCodec;
		posix_time::ptime lastTerrainUpdate;

		// Transfer manager.
		boost::shared_ptr<TransferManager> transferManager;

		// Threading.
		boost::mutex stateMutex;
	};

}; // namespace XernMetaverse

#endif //XERN_CLIENT_SESSION_HPP
